package com.ldc.wandroid.ui.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.IntentUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.SystemInfoAdapter;
import com.ldc.wandroid.databinding.ActivitySystemInfoBinding;
import com.ldc.wandroid.model.ArticleModel;
import com.ldc.wandroid.model.SystemInfoModel;
import com.ldc.wandroid.viewmodel.SystemInfoViewModel;

public class SystemInfoActivity extends BaseMvvmActivity<ActivitySystemInfoBinding, SystemInfoViewModel> {
    private static final String ARG_NAME = "name";
    private static final String ARG_CID = "cid";
    private String cid;
    private String name;
    private int currIndex;
    private SystemInfoAdapter systemInfoAdapter;

    public static void actionStart(Activity activity, String name, String cid) {
        Intent intent = new Intent(activity, SystemInfoActivity.class);
        intent.putExtra(ARG_NAME, name);
        intent.putExtra(ARG_CID, cid);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewModel.getSystemInfo().removeObserver(this::getSystemInfo);
    }

    @Override
    protected ActivitySystemInfoBinding initViewBinding() {
        return ActivitySystemInfoBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        cid = IntentUtils.getString(this, ARG_CID);
        name = IntentUtils.getString(this, ARG_NAME);
        initTopBar();
        initAdapter();
        viewModel.getSystemInfo().observeForever(this::getSystemInfo);
        viewModel.getSystemInfo(currIndex, cid);
    }

    @Override
    protected SystemInfoViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, SystemInfoViewModel.class);
    }

    private void initTopBar() {
        viewBinding.topBar.tvTitle.setText(name);
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        viewBinding.topBar.lineBack.setOnClickListener(view -> {
            finish();
        });

    }

    private void initAdapter() {
        viewBinding.rv.refreshView.setEnableRefresh(false);
        viewBinding.rv.refreshView.setOnLoadMoreListener(refreshLayout -> {
            currIndex += 1;
            viewModel.getSystemInfo(currIndex, cid);
        });
        if (systemInfoAdapter == null) {
            systemInfoAdapter = new SystemInfoAdapter();
        }
        systemInfoAdapter.addRowCollectionClick(data -> {
            if (data.isCollect()) {
                viewModel.unSelectCollect(data.getId() + "");
            } else {
                viewModel.selectCollect(data.getId() + "");
            }
        });
        systemInfoAdapter.addRowClick(data -> {
            ShowArticleWebActivity.actionStart(this, data.getTitle(), data.getProjectLink());
        });
        viewBinding.rv.recyclerView.setAdapter(systemInfoAdapter);
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }


    private void getSystemInfo(ArticleModel data) {
        viewBinding.stateView.showContentView();
        viewBinding.rv.refreshView.finishLoadMore();
        if (data == null) {
            return;
        }
        viewBinding.rv.refreshView.setNoMoreData(currIndex == data.getPageCount());
        systemInfoAdapter.addNewData(data.getDatas());

    }
}
